/*
 * Login.java
 *
 * Created on November 9, 2007, 4:35 PM
 */

package bloggerdesktop;
import com.google.gdata.client.*;
//import com.google.gdata.client.calendar.*;
import com.google.gdata.data.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;
import java.awt.Desktop;  //Used for link creation - requires java 6
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author  Reimius Forunner
 */
public class Login extends javax.swing.JFrame {
    /** Creates new form Login */
    static Login ine;
    Desktop linkSrv = Desktop.getDesktop();  //Requires Java 6
    public Login() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        linkLabel = new javax.swing.JLabel();
        uIDLogin = new javax.swing.JTextField();
        uPWLogin = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        linkLabel.setText("<html><a href=\"\">No Account? Click Here!</a></html>");
        linkLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                openLink(evt);
            }
        });

        uPWLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                EnterCheck(evt);
            }
        });

        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("UserName");

        jLabel2.setText("Password");

        jLabel3.setText("Blogger Login.");

        jButton2.setText("Quit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(linkLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(uPWLogin)
                            .add(uIDLogin, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jButton1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton2))))
                    .add(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .add(27, 27, 27)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(uIDLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(uPWLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton2)
                        .add(jButton1))
                    .add(linkLabel))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void EnterCheck(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_EnterCheck
        if (evt.getKeyCode()==KeyEvent.VK_ENTER){
            jButton1.setSelected(true);
            jButton1ActionPerformed(new ActionEvent(this,0,"do it."));
        }
    }//GEN-LAST:event_EnterCheck

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            uIDLogin.setEnabled(false);
            uPWLogin.setEnabled(false);
            String passwd = "";
            for (char i : uPWLogin.getPassword()) {
                passwd += i;
            }
            String userID = uIDLogin.getText();
            MainMenu.defaultPerson = new Person(userID);
            MainMenu.myService.setUserCredentials(userID, passwd);
            MainMenu.mainStart();
        } catch (AuthenticationException ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Error: \n" + 
                    ex.getMessage());
            uIDLogin.setEnabled(true);
            uPWLogin.setEnabled(true);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void exit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exit
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_exit

    private void openLink(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openLink
        try {
            // TODO add your handling code here:
            URI link = new URI("https://www.google.com/accounts/NewAccount?continue=https%3A%2F%2Fwww.blogger.com%2Floginz%3Fd%3Dhttps%253A%252F%252Fwww.blogger.com%252Fhome%26a%3DADD_SERVICE_FLAG&service=blogger&hl=en");
            linkSrv.browse(link);
        }catch (Exception ex){
//        } catch (Exception ex) {//GEN-LAST:event_openLink
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public void start() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ine = new Login();
                ine.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel linkLabel;
    private javax.swing.JTextField uIDLogin;
    private javax.swing.JPasswordField uPWLogin;
    // End of variables declaration//GEN-END:variables
    
}
